//
//  HotSongTableViewCell.m
//  DolphinMusic
//
//  Created by dllo on 15/3/22.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "HotSongTableViewCell.h"
#import "Song.h"
#import "MyDownLoadViewController.h"
#import "NetHandler.h"


@implementation HotSongTableViewCell

- (void)dealloc
{
    [_downButtonImageView release];
    [_str release];
    [_receiveData release];
    [_titleLabel release];
    [_hotSong release];
    [_singerImageView release];
    [_moreButton release];
    [_reasonImageView release];
    [_reasonImageView release];
    [_authorLabel release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.singerImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.singerImageView];
        [_singerImageView release];
        
        self.titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.titleLabel];
        [_titleLabel release];
        
        self.authorLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.authorLabel];
        [_authorLabel release];
        
        self.reasonImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.reasonImageView];
        [_reasonImageView release];
        
        self.recommend_reasonLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.recommend_reasonLabel];
        [_recommend_reasonLabel release];
        
        self.moreButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.moreButton addTarget:self action:@selector(moreButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.moreButton];
        
        self.downButtonImageView = [[UIImageView alloc] init];
        [self.moreButton addSubview:self.downButtonImageView];
        [_downButtonImageView release];
        
    }
    return self;
}


- (void)moreButtonAction:(UIButton *)button
{
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(button.frame.origin.x - 60, self.downButtonImageView.frame.origin.y, 60, 20)];
    label.text = @"开始下载";
    label.font = [UIFont systemFontOfSize:12];
    label.textColor = [UIColor lightGrayColor];
    label.textAlignment =  NSTextAlignmentCenter;
    label.layer.cornerRadius = 5;
    label.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    label.layer.borderWidth = 1;
    [self.contentView addSubview:label];
    [label release];
    
    DataBaseHandler *dbHandler = [DataBaseHandler shareInstance];
    
    NSMutableArray *arr = [dbHandler selectAllInFilePath];
    for (Song *song in arr) {
        if ([song.song_id isEqualToString:self.hotSong.song_id]) {
            self.ifDownLoad = YES;
        }
    }
    
    if (self.ifDownLoad == YES) {
        label.text = @"已下载";
    }
    else {
       label.text = @"开始下载";
    [dbHandler downLoadSongWithSongID:self.hotSong.song_id];
    }
    
    
    [UIView transitionWithView:label duration:3 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        label.alpha = 0;
    } completion:^(BOOL finished) {
        
    }];
    
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    
    self.singerImageView.frame = CGRectMake(0, 5, 70, 70);
    NSURL *url = [NSURL URLWithString:[self.hotSong.pic_small stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    
    [self.singerImageView setImageWithURL:url];
    
    self.titleLabel.frame = CGRectMake(80, 5, 250, 20);
    self.titleLabel.text = self.hotSong.title;
    self.titleLabel.font = [UIFont systemFontOfSize:16];
    
    self.authorLabel.frame = CGRectMake(80, 30, 250, 18);
    self.authorLabel.text = self.hotSong.author;
    self.authorLabel.font = [UIFont systemFontOfSize:14];
    
    self.reasonImageView.frame = CGRectMake(80, 55, 20, 20);
    self.reasonImageView.image = [UIImage imageNamed:@"recommendReason.png"];
    
    self.recommend_reasonLabel.frame = CGRectMake(105, 55, self.contentView.frame.size.width - 110, 20);
    self.recommend_reasonLabel.alpha = 0.6;
    self.recommend_reasonLabel.text = self.hotSong.recommend_reason;
    self.recommend_reasonLabel.font = [UIFont systemFontOfSize:13];
    
    self.moreButton.frame = CGRectMake(self.contentView.frame.size.width - 50, 0, 50, 50);
    
    self.downButtonImageView.frame = CGRectMake(15, 15, 20, 20);
    self.downButtonImageView.image = [UIImage imageNamed:@"weixiazai1.png"];
    
}






- (void)awakeFromNib {
    // Initialization code
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
    
    
    
    
    
    
}

@end
